package cn.com.nes.site.modules.opportuntiy;

import cn.com.nes.mybatis.agent.crm.entity.Opportunity;
import cn.com.nes.site.entity.em.crm.opportunity.OpportunityClassificationEnum;
import cn.com.nes.site.service.crm.opportunity.CrmOpportunityService;
import cn.com.nes.site.service.crm.opportunity.bo.GetOpportunityDetailBO;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityBO;
import cn.com.nes.site.service.crm.opportunity.bo.QueryOpportunityPageBO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityDetailDTO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityInfoDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OpportunityBusiness {

    @Resource
    private OpportunityHandleFactory opportunityHandleFactory;

    @Resource
    private CrmOpportunityService crmOpportunityService;

    public Opportunity getOpportunity(String customerId, String opportunityId) {
        return crmOpportunityService.getOpportunity(customerId, opportunityId);
    }

    public Opportunity getOpportunity(String opportunityId) {
        return crmOpportunityService.getById(opportunityId);
    }

    private OpportunityHandle getOpportunityHandle(OpportunityClassificationEnum opportunityClassification) {
        return opportunityHandleFactory.getInstance(opportunityClassification);
    }

    public Opportunity save(OpportunityBO opportunityBO) throws Exception {
        return getOpportunityHandle(opportunityBO.getOpportunityClassification()).save(opportunityBO);
    }

    public Page<OpportunityInfoDto> page(QueryOpportunityPageBO queryOpportunityPageBO) throws Exception {
        return getOpportunityHandle(queryOpportunityPageBO.getOpportunityClassificationEnum()).page(queryOpportunityPageBO);
    }

    public OpportunityDetailDTO details(GetOpportunityDetailBO bo) throws Exception {
        Opportunity opportunity = crmOpportunityService.getById(bo.getOpportunityId());
        return getOpportunityHandle(OpportunityClassificationEnum.getEnum(opportunity.getOpportunityClassification())).details(bo);
    }

    public void deleted() {

    }


    public Opportunity getOpportunityByName(String customerUuid, String opportunityName) throws Exception {
        return crmOpportunityService.getOpportunityByName(customerUuid, opportunityName);
    }

}
